import OpenAI from "openai";
import express from "express";
import cors from "cors";  // 导入 CORS 中间件

const app = express();
const PORT = 8014;

// 使用 CORS 中间件来解决跨域问题
app.use(cors());


async function get_completion(messages_,model_){
  const openai = new OpenAI({
    baseURL: 'https://api.deepseek.com',
    apiKey: 'sk-aaf5569c824c40a993e44820f90d2005'
  });
  const completion = await openai.chat.completions.create({
    messages: messages_,
    model:model_,
  });
  return completion;
}

// 解析 JSON 请求体
app.use(express.json());
app.post('/api/deepseek', async (req, res) => {
  const { messages, model } = req.body;
  const data = await get_completion( messages, model);
  res.send(data);
});

// 解析 JSON 请求体
app.use(express.json());
app.get('/api/get', async (req, res) => {
  res.send("你好");
});

// 启动服务器
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`);
});
